package com;

import util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(value = "/StudentScore")
public class StudentScore extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        int u_id= (int) request.getSession().getAttribute("u_id");
        String  role= (String) request.getSession().getAttribute("role");
        String html="";
        Connection con=null;
        PreparedStatement psm=null;
        ResultSet rs=null;
        try {
            con = JdbcUtil.getConnection();
            psm = con.prepareStatement("SELECT sc.s_id,sc.c_id,sc.degree FROM score sc JOIN USER u ON u.`u_id`=sc.`s_id` WHERE u.`u_id`="+u_id);
            rs=psm.executeQuery();
            html = html+"<table border='1' width = 500px>";
            html = html+"<tr><td>学号</td><td>课程号</td><td>成绩</td></tr>";
            while (rs.next()){
                int studentid=rs.getInt("sc.s_id");
                int courseid=rs.getInt("sc.c_id");
                int degree=rs.getInt("sc.degree");

                html = html+"<tr>" +
                        "<td>"+studentid+"</td>" +
                        "<td>"+courseid+"</td>" +
                        "<td>"+degree+"</td>" +
                        "</tr>";

                    }html =html+ "</table>";
                    rs.close();

            } catch (SQLException e1) {
            e1.printStackTrace();
        }
        response.getWriter().write(html);
    }

}




